package dswork.html.select;

import dswork.html.nodes.Node;

public class NodeTraversor
{
	private NodeVisitor visitor;

	public NodeTraversor(NodeVisitor visitor)
	{
		this.visitor = visitor;
	}

	public void traverse(Node root)
	{
		Node node = root;
		int depth = 0;
		while(node != null)
		{
			visitor.head(node, depth);
			if(node.childNodeSize() > 0)
			{
				node = node.childNode(0);
				depth++;
			}
			else
			{
				while(node.nextSibling() == null && depth > 0)
				{
					visitor.tail(node, depth);
					node = node.parentNode();
					depth--;
				}
				visitor.tail(node, depth);
				if(node == root)
					break;
				node = node.nextSibling();
			}
		}
	}
}
